import java.io.*;

class FileDemo {

	public static void main(String[] args) throws Exception {
		// test1();
		// test2();
		// test3();
		test7();
	}
	// 复制图片文件
	public static void test7() {
		File file = new File("图片1.jpg");
		if (file.exists()) {
			
			try {
				FileInputStream fis = new FileInputStream(file);
				FileOutputStream fos = new FileOutputStream("图片3.jpg");
				// 一次读一组字节
				byte[] buf = new byte[1024];
				// 将读出来的内容存入buf数组
				int length = fis.read(buf);
				while(length != -1) {
					// 将标准输入，改成文件输出即可
					// fos.write(buf); // 有缺陷，最后一次会多写入一些字节
					fos.write(buf, 0, length);
					length = fis.read(buf);
				}
				fos.close();
				fis.close();
			} catch (Exception e) {
				System.err.println(e.getMessage());
			}
		}
	}

	// 改成字节流来实现文件读取
	public static void test6() {
		File file = new File("图片1.jpg");
		if (file.exists()) {
			
			try {
				FileInputStream fis = new FileInputStream(file);
				
				// 一次读一组字节
				byte[] buf = new byte[1024];
				// 将读出来的内容存入buf数组
				int length = fis.read(buf);
				while(length != -1) {
					System.out.println(new String(buf, 0 , length));
					length = fis.read(buf);
				}
			} catch (Exception e) {
				System.err.println(e.getMessage());
			}
		}
	}

	public static void test5() throws Exception {
		// 问件复制思路
		// 用FileReader将文件读入到内存
		// 再将内存中的内容用FileWriter写入一个新文件
		FileWriter fileWriter = new FileWriter("writer.txt");
	    fileWriter.write("轻轻的我走了，\r\n");
	    fileWriter.write("正如我轻轻的来；\r\n");
	    fileWriter.write("我轻轻的招手，\r\n");
	    fileWriter.write("作别西天的云彩。\r\n");
	    fileWriter.close();

	}

	public static void test4(){
		File file = new File("readme2.txt");
		if (file.exists()) {
			try {
				FileWriter wr = new FileWriter(file, true);
				wr.write('A');
				// 如果是写文件，一定要正确关闭后才能写入
				wr.close();
			} catch(Exception e) {
				System.err.println(e.getMessage());
			}
		}
	}

	public static void test3() {
		File file = new File("readme2.txt");
		if (file.exists()) {
			
			try {
				FileReader reader = new FileReader(file);
				// 单字符读取，性能差
				/*int result = reader.read();
				while (result != -1) {
					// print不换行
					// println要换行
					System.out.print((char)result);
					result = reader.read();
				} */
				// 一次读一组字符
				char[] buf = new char[1024];
				// 将读出来的内容存入buf数组
				int length = reader.read(buf);
				while(length != -1) {
					System.out.println(new String(buf, 0 , length));
					length = reader.read(buf);
				}
			} catch (Exception e) {
				System.err.println(e.getMessage());
			}
		}
	}

	public static void test2() {
		File dir = new File("E:\\2020-2021-2\\workspace\\week13");
		if (dir.isDirectory()) {
			File[] files = dir.listFiles();
			for (File file : files) {
				System.out.println(file.getName());
			}
		}
	}

	public static void test1() {
		File file = new File("readme.txt");
		if (file.exists()) {
			System.out.println(file.getAbsolutePath());

			File newFile = new File("readme2.txt");
			boolean flag = file.renameTo(newFile);
			if (flag) {
				System.out.println("改名成功");
			} else {
				System.out.println("改名失败");
			}
		} else {
			try {
				boolean f = file.createNewFile();
				System.out.println(f);
			} catch (IOException e) {
				System.err.println(e.getMessage());
			}
		}
	}
}